/*
 * Copyright (C) 2008 Derek James and Philip Tucker
 * 
 * This file is part of ANJI (Another NEAT Java Implementation).
 * 
 * ANJI is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * created by Philip Tucker on Jul 6, 2004
 */

package com.anji.moorph;

import java.awt.Canvas;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventListener;
import java.util.List;

import javax.swing.JFrame;

/**
 * @author Philip Tucker
 */
public class MoorphChooser extends JFrame {

  private static final long serialVersionUID = -2378388227451368412L;

  private final static String NAME = "Moorph Display";
  
  public static interface ChoiceListener extends EventListener {
    public void choose(int index);
  }

  private static class MouseClickListener implements MouseListener {

    private final int index;

    private final ChoiceListener choiceListener;

    private final MoorphChooser chooser;

    public MouseClickListener(MoorphChooser container, int index, 
        ChoiceListener choiceListener) {
      this.index = index;
      this.choiceListener = choiceListener;
      this.chooser = container;
    }

    public void mouseClicked(MouseEvent e) {
      choiceListener.choose(index);
      chooser.onUserSelection();
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

  }

  /**
   * ctor
   */
  public MoorphChooser(ChoiceListener choiceListener, List<Image> images) {
    super(NAME);

    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    setAlwaysOnTop(true);
    setName(NAME);
    setResizable(false);
    setUndecorated(true);
    
    Container contentPane = getContentPane();

    int gridDimension = (int) (Math.sqrt(images.size()) + 0.5);
    GridLayout mainLayout = new GridLayout(gridDimension, gridDimension);
    mainLayout.setHgap(5);
    mainLayout.setVgap(5);
    contentPane.setLayout(mainLayout);

    // size of gaps
    int width = 5 * (images.size() + 1);
    int height = 5 * (images.size() + 1);

    for (int i = 0; i < images.size(); ++i) {
      Canvas canvas = new ImageCanvas(images.get(i));
      
      // size of images
      width += canvas.getWidth();
      height += canvas.getHeight();

      canvas.addMouseListener(new MouseClickListener(this, i, choiceListener));
      contentPane.add(canvas);
    }
    
    Dimension dim = new Dimension(width, height);
    contentPane.setMinimumSize(dim);
    contentPane.setSize(dim);

    pack();
  }

  /**
   * @return Number images handled by this chooser.
   */
  public int getImageCount() {
    return getContentPane().getComponentCount();
  }
  
  /**
   * Block until user responds.
   */
  public synchronized void waitForUser() {
    try {
      wait();
    } catch (InterruptedException ex) {
      throw new IllegalArgumentException("waiting for user", ex);
    }
  }
  
  /**
   * Block until user responds.
   */
  public synchronized void onUserSelection() {
    notify();
    setVisible(false);
    dispose();
  }

  /**
   * @see java.lang.Object#toString()
   */
  public String toString() {
    return NAME;
  }
}
